package com.download.utils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtil {

    private HttpUtil(){
    }
    /**
     * 字符串url创建链接
     * @param urlPath
     * @return HTTP连接
     * @throws IOException 
     */
    public static HttpURLConnection getHttpConnection(String urlPath) throws IOException {
        URL url = new URL(urlPath);
        return getHttpConnection(url);
    }
    
    /**
     * 通过URL获取链接
     * @param url
     * @return HTTP连接
     * @throws IOException 
     */
    public static HttpURLConnection getHttpConnection(URL url) throws IOException {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        setRequestProperty(conn);
        conn.setConnectTimeout(0);
        conn.setReadTimeout(0);
        return conn;
    }
    
    /**
     * 根据请求头中是否有Range字段，获取HTTP状态码
     * @param url
     * @param range
     * @return HTTP状态码
     * @throws IOException
     */
    public static int getResponseCode(String url, boolean range) throws IOException {
        HttpURLConnection conn = getHttpConnection(url);
        setRequestProperty(conn);
        if(range){
            conn.setRequestProperty("Range", "bytes=0-");
        }
        int responseCode = conn.getResponseCode();
        disconnect(conn);
        return responseCode;
    }
    
    /**
     * 设置部分请求头
     * @param conn
     * @return 返回设置完请求头的连接
     * @throws Exception
     */
    public static HttpURLConnection setRequestProperty(HttpURLConnection conn) {
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 UBrowser/3.0.1644.0 Safari/537.36");
        conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
        conn.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
        conn.setRequestProperty("Keep-Alive", "on");
        conn.setRequestProperty("Connection", "keep-alive");
        return conn;
    }
    
    /**
     * 打印响应信息
     * @param conn
     * @throws IOException 
     */
    public static synchronized void printResponseInfo(HttpURLConnection conn) throws IOException {
        if (conn.getResponseCode() >= 400) {
            for (int i = 0; i < conn.getHeaderFields().size(); i++) {
                if (i==0) 
                    LogUtil.info(conn.getHeaderField(conn.getHeaderFieldKey(0)));
                else
                    LogUtil.info(conn.getHeaderFieldKey(i)+"="+conn.getHeaderField(conn.getHeaderFieldKey(i)));
                }
        } else {
            for (int i = 0; i < conn.getHeaderFields().size(); i++) {
                if (i==0) 
                    LogUtil.info(conn.getHeaderField(conn.getHeaderFieldKey(0)));
                else
                    LogUtil.info(conn.getHeaderFieldKey(i)+"="+conn.getHeaderField(conn.getHeaderFieldKey(i)));
            }
        }
        disconnect(conn);
    }
    
    /**
     * 打印请求头
     * @param conn
     */
    public static void printRequestInfo(HttpURLConnection conn) {
        LogUtil.info(conn.getRequestProperties());
    }
    
    /**
     * 返回文件长度
     * @param conn
     * @return 文件长度
     */
    public static long getContentLength(HttpURLConnection conn) {
        return conn.getContentLengthLong();
    }
    
    /**
     * 断开连接
     * @param conn
     */
    public static void disconnect(HttpURLConnection conn) {
        conn.disconnect();
    }
}
